//Pascale Vacher - March 12
//OOP Assignment Semester 2

#include "UserInterface.h" 

//---------------------------------------------------------------------------
//UserInterface: class implementation
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
//public member functions
//---------------------------------------------------------------------------

//____constructors & destructors

//____other public member functions

//menu functions

void UserInterface::wait() const {
	char ch;
	cout << "\n\nPress RETURN to go back to menu\n";
	cin.get( ch);
	cin.get( ch);
//	flushall();
}
int UserInterface::readInCardIdentificationCommand() const {
    showCardIdentificationMenu();
    return ( readInCommand());
}
void UserInterface::showCardIdentificationMenu() const {
	cout << "\n\n\n      ________________________________________";
	cout << "\n      _______CARD IDENTIFICATION MENU________";
	cout << "\n      ________________________________________";
	cout << "\n       0           Quit CashPoint application";
	cout << "\n       1              Enter your card details";
	cout << "\n      ________________________________________";
}
int UserInterface::readInAccountProcessingCommand() const{
    showAccountProcessingMenu();
    return ( readInCommand());
}
void UserInterface::showAccountProcessingMenu() const {
	cout << "\n\n\n      ________________________________________";
	cout << "\n      ________ACCOUNT PROCESSING MENU________";
	cout << "\n      ________________________________________";
	cout << "\n       0 End account processing & remove card";
	cout << "\n       1                      Display balance";
	cout << "\n       2                Withdraw from account";
	cout << "\n       3                 Deposit into account";
	cout << "\n       4                       Show statement";
	cout << "\n       5                    Show all deposits  //IMPLEMENTED";
	cout << "\n       6                  Show mini statement  //IMPLEMENTED";
	cout << "\n       7    Show all transations above amount  //IMPLEMENTED";
	cout << "\n       8    Clear all transactions up to date  //IMPLEMENTED";
	cout << "\n       9          Transfer to another account  //TO DO";
	cout << "\n         ________________________________________";
}
void UserInterface::showSearchSubMenu() const {
	cout << "\n\n\n      ________________________________________";
	cout << "\n      ________SEARCH FOR TRANSACTIONS_________";
	cout << "\n      ________________________________________";
	cout << "\n       A	                    by Amount";
	cout << "\n       B                             by Title";
	cout << "\n       C                              by Date";
	cout << "\n       D	                         Exit";
	cout << "\n      ________________________________________";
}
char UserInterface::readInSearchSubMenuCommand() const {
	showSearchSubMenu();
	return ( readInLetterCommand() );
}
const string UserInterface::readInCardToBeProcessed( string& aCardNumber) const {
	cout << "\n SELECT THE CARD ...\n";
	cout << "   CARD NUMBER:  ";         //ask for card number
    cin >> aCardNumber;
    cout << "\n=========================================";
    //create card file name
    return( "card_" + aCardNumber + ".txt");
}

void UserInterface::showValidateCardOnScreen( bool valid, const string& cashCardNum) const {
	if ( ! valid)   //invalid card
        cout << "\nERROR: INVALID CARD\n"
             << "\nTHE CARD (NUMBER: " << cashCardNum << ") DOES NOT EXIST!";
    else   //card exists
        cout << "\nTHE CARD (NUMBER: " << cashCardNum << ") EXIST!";
}

void UserInterface::showCardOnScreen( const string& aCardDetails) const {
    cout << "\n=========================================";
    cout << "\n________ CARD DETAILS ___________________";
	cout << aCardDetails;
    cout << "\n________ END CARD DETAILS _______________";
    cout << "\n=========================================";
}

const string UserInterface::readInAccountToBeProcessed( string& anAccountNumber, string& aSortCode) const {
    cout << "\n SELECT THE ACCOUNT ...\n";
	cout << "   ACCOUNT NUMBER:  ";	//ask for account number
    cin >> anAccountNumber;
	cout << "   SORT CODE:       ";	//ask for sort code
    cin >> aSortCode;
    cout << "\n=========================================";
    //create account file name
    return( "account_" + anAccountNumber + "_" + aSortCode + ".txt");
}
void UserInterface::showValidateAccountOnScreen( int validCode, const string& accNum, const string& srtCode) const {
    switch( validCode)
    {
    	case VALID_ACCOUNT:		//account valid: it exists, is accessible with that card (and not already open: TO BE IMPLEMENTED)
        	cout << "\nTHE ACCOUNT (NUMBER: " << accNum
             	 << " CODE: " << srtCode << ") IS NOW OPEN!";
            break;
    	case UNKNOWN_ACCOUNT:		//account does not exist
        	cout << "\nERROR: INVALID ACCOUNT"
                 << "\nTHE ACCOUNT (NUMBER: " << accNum
                 << " CODE: " << srtCode << ") DOES NOT EXIST!";
            break;
    	case UNACCESSIBLE_ACCOUNT:		//account exists but is not accessible with that card
        	cout << "\nERROR: INVALID ACCOUNT"
             	 << "\nTHE ACCOUNT (NUMBER: " << accNum
             	 << " CODE: " << srtCode << ") IS NOT ACCESSIBLE WITH THIS CARD!";
            break;
    }
}
void UserInterface::showAllDepositsOnScreen(bool noTransactions, string str, double total) const{
	if ( noTransactions == false){
		Time time;
		Date date;

		cout << "\nALL DEPOSIT TRANSACTIONS REQUESTED AT "<< time.currentTime() << " ON " << date.currentDate();
		cout << "\n" << str;
		cout << "\n\tTOTAL: \234" << total;
	}
	else
		cout << "\nTHERE ARE NO DEPOSIT TRANSACTIONS";
}
void UserInterface::showMatchingTransactionsOnScreen( string searchParam, int noOfTransactions, string str) const{
	if ( noOfTransactions < 1 )
		cout << "\nNO TRANSACTIONS MATCH THE SEARCH CRITERION GIVEN";
	else {
		cout << "\nTHERE ARE " << noOfTransactions << " TRANSACTIONS MATCHING '" << searchParam << "':";
		cout << "\n" << str;
	}
}


//input functions
int UserInterface::readInNumberOfTransactions() const{
	cout << "\nENTER THE NUMBER OF TRANSACTIONS YOU WOULD LIKE TO SEE: ";
	return readInPositiveNumber();
}
double UserInterface::readInWithdrawalAmount() const {
    //ask for the amount to withdraw
    cout << "\nAMOUNT TO WITHDRAW: \234" ;
	return ( readInPositiveAmount());
}
double UserInterface::readInDepositAmount() const {
    //ask for the amount to deposit
    cout << "\nAMOUNT TO DEPOSIT: \234" ;
	return ( readInPositiveAmount());
}
double UserInterface:: readInAmount() const {
	double amount;
	cout << "\nSEARCH FOR TRANSACTIONS BY AMOUNT";
	cout << "\nENTER AMOUNT: ";
	cin >> amount;
	return ( amount);
}
string UserInterface:: readInTitle() const{
	cout << "\nSEARCH FOR TRANSACTIONS BY TITLE";
	cout << "\nENTER TITLE: ";
	return ( readInString() );
}
string UserInterface:: readInDate() const{
	cout << "\nSEARCH FOR TRANSACTIONS BY DATE";
	cout << "\nENTER DATE (eg: DD/MM/YYYY): ";
	return ( readInString() );
}
Date UserInterface::readInValidDate(const Date cd) const {
	Date aDate;
	cout << "\nENTER A DATE (eg DD/MM/YYYY): ";
	cin >> aDate;
	while( !aDate.isValidDate(cd) ){
		cout << "\nMAKE SURE THE DATE YOU ARE ENTERING IS VALID, TRY AGAIN: ";
		cin >> aDate;
	}
	return aDate;
}
bool UserInterface::readInConfirmDeletion() const{
	char answer;
	cout << "ARE YOU SURE YOU WANT TO DELETE THESE TRANSACTIONS? (Y/N): ";
	cin >> answer;
	if (toupper(answer) == 'Y')
		return true;
	return false;
}

double UserInterface::readInTransferAmount() const {
	cout << "PLEASE ENTER THE AMOUNT YOU WISH TO TRANSFER : ";
	return readInPositiveAmount();
}

//output functions

void UserInterface::showProduceBalanceOnScreen( double balance) const {
    cout << "\nTHE CURRENT BALANCE IS: \234" << setw(12) << balance;
}
void UserInterface::showWithdrawalOnScreen( bool trAuthorised, double amountWithdrawn) const {
    if ( trAuthorised)
    	cout << "\nTRANSACTION AUTHORISED!: \n\234"
             << setw(0) << amountWithdrawn
             << " WITHDRAWN FROM ACCOUNT";
    else //not enough money
		cout << "\nTRANSACTION IMPOSSIBLE!";
}
void UserInterface::showDepositOnScreen( bool trAuthorised, double amountDeposited) const {
    if ( trAuthorised)
    	cout << "\nTRANSACTION AUTHORISED!:\n\234"
             << setw(0) << amountDeposited
             << " DEPOSITED INTO ACCOUNT";
    else //too much to deposit
		cout << "\nTRANSACTION IMPOSSIBLE!";
}
void UserInterface::showStatementOnScreen( const string& statement) const {
    cout << "\nPREPARING STATEMENT...";
    cout << "\n________ ACCOUNT STATEMENT _____";
	cout << statement;
    cout << "\n________ END ACCOUNT STATEMENT _____";
}
void UserInterface::showMiniStatementOnScreen( bool noTransactions, double total, string str) const{
	cout << "\nPREPARING MINI STATEMENT...";
	if ( !noTransactions ) {
		Time time;
		Date date;
		cout << "\nRECENT TRANSACTIONS REQUESTED AT " << time.currentTime() << " on " << date.currentDate();
		cout << "\n" << str;
		cout << "TOTAL: " << total;
	}
	else
	{
		cout << "THERE ARE NO TRANSACTIONS IN BANK ACCOUNT";
	}
}
void UserInterface::showTransactionsUpToDateOnScreen( bool noTransactions, Date date, int numOfTransactions, string str) const{
	cout << "\nTHERE ARE " << numOfTransactions << " TRANSACTIONS UP TO AND INCLUDING " << date.toFormattedString() << ":";
	cout << "\n" << str;
}

void UserInterface::showDeletionOfTransactionUpToDateOnScreen( int numOfTransactions, Date date, bool deletionConfirmed) const {
	if ( deletionConfirmed){
		cout << numOfTransactions << " TRANSACTIONS HAVE BEEN DELETED UP TO " << date;
	} else {
		cout << "DELETION CANCELLED";
	}
}

void UserInterface::showMessageOnScreen(const string message) const {
	cout << "\n" << message;
}

void UserInterface::showTransferOnScreen(const bool canTransferOut, const bool canTransferIn, const double transferAmount) const {
	if(!canTransferIn) {
		cout << "CANNOT TRANSFER INTO THE REQUESTED ACCOUNT";
	}

	if(!canTransferOut) {
		cout << "CANNOT TRANSFER FROM THE REQUESTED ACCOUNT";
	}
	
	if (canTransferIn && canTransferOut) {
		cout << "\nTHE AMOUNT OF \234" << transferAmount << " WAS SUCCESSFULLY TRANSFERED";
	}
}

//---------------------------------------------------------------------------
//private support member functions
//---------------------------------------------------------------------------

void UserInterface::showWelcomeScreen() const {
	cout << "\n\n\n             _____WELCOME TO THE ATM_____";
}
void UserInterface::showByeScreen() const {
    cout << "\n\n\n________________NEXT CUSTOMER...\n\n";
}
int UserInterface::readInCommand() const{
	int com;
	cout << "\n          ENTER YOUR COMMAND: ";
	cin >> com;
	return com;
}
char UserInterface::readInLetterCommand() const{
	char com;
	cout << "\n          ENTER YOUR COMMAND: ";
	cin >> com;
	return toupper(com);
}
void UserInterface::showErrorInvalidCommand() const {
	cout << "\nINVALID COMMAND CHOICE, TRY AGAIN";
}
double UserInterface::readInPositiveAmount() const {
    double amount;
	cin >> amount;
	while ( amount <= 0.0)
    {
	    cout << "\nAMOUNT SHOULD BE A POSITIVE AMOUNT, TRY AGAIN: ";
		cin >> amount;
	}
    return amount;
}
string UserInterface::readInString() const {
	string str;
	cin >> str;
	return str;
}
int UserInterface::readInPositiveNumber() const {
	int number;
	cin >> number;
	while ( number <= 0 )
	{
		cout << "\nPLEASE ENTER A POSITIVE NUMBER, TRY AGAIN: ";
		cin >> number;
	}
	return number;
}
Time UserInterface::currentTime() const{
	Time time;
	return time.currentTime();
}
Date UserInterface::currentDate() const{
	Date date;
	return date.currentDate();
}